package com.ono;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class GenColor {

    public static void main(String[] args) throws IOException {

        StringBuilder sb = new StringBuilder();
        sb.append("<html>");
        sb.append("<head>");
        sb.append("<meta charset=\"UTF-8\"><title>ONO model list</title>"
            + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"
            + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>");
        sb.append("</head>");
        sb.append("<body>");

        sb.append("<table cellspacing=\"0\" border=\"1px\" style=\"width:80%;margin: 0 auto;border: solid 1px #ddd\">");
        BufferedReader in = new BufferedReader(new FileReader("data/color.txt"));
        String str;
        int index = 0;
        boolean start = false;
        while ((str = in.readLine()) != null) {
            System.out.println(str);
            if(!start && index%4 == 0) {
                sb.append("<tr>");
                start = true;
            }
           index++;
           String[] data= str.split("\\s");
           sb.append("<td><span><div style=\"background-color:"+data[2]+";display: inline-block;padding-right:20%;\">&nbsp;</div>"+data[2]+" "+data[1]+"</span></td>");
           if(start && index%4 == 0) {
               sb.append("</tr>");
               start = false;
           }
         
        }
        in.close();
        sb.append("</table>");
        sb.append("</body>");
        sb.append("</html>");
        writeFile("data/color.html", sb.toString());

    }

    public static void writeFile(String filePath, String content) {
        FileWriter writer = null;
        try {
            writer = new FileWriter(filePath);
            writer.write("");// 清空原文件内容
            writer.write(content);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
